<?php
class ChannelCategoryModel
{
    /*
        添加第三方分类
        params
            channel_id 第三方id
            store_id 店铺id

            category_id 本地分类id

            other_category_id 第三方分类id
            other_category_name 第三方分类名称
            other_category_rank 第三方分类排序
    */
    public function addCategoryInfo($channel_id = 0, $store_id = 0, $category_id = 0, $other_category_id = 0, $other_category_name = '', $other_category_rank = 0)
    {
        $Dao = M("kfsy_channel_store.kf_channel_category");

        $data = array();
        $data['fk_channel_id'] = $channel_id;
        $data['fk_store_id'] = $store_id;
        $data['fk_category_id'] = $category_id;
        $data['other_category_id'] = $other_category_id;
        $data['other_category_name'] = $other_category_name;
        $data['other_category_rank'] = $other_category_rank;
        $data['createtime'] = date("Y-m-d H:i:s");

        $add = $Dao->add($data);

        if($add == false){
            DLOG("添加第三方分类失败 sql: " . $Dao->getLastSql(), "error", "channel_db");
            return false;
        }

        return $add;
    }

    /*
        更新第三方分类信息
        params
            other_category_id 第三方分类id
            other_category_name 第三方分类名称
            other_category_rank 第三方分类排序
    */
    public function updateCategoryInfo($other_category_id = 0, $other_category_name = '', $other_category_rank = 0)
    {
        $Dao = M("kfsy_channel_store.kf_channel_category");

        $where = sprintf("other_category_id = %s", $other_category_id);

        $data = array();
        $data['other_category_name'] = $other_category_name;
        $data['other_category_rank'] = $other_category_rank;

        $save = $Dao
            ->where($where)
            ->save($data);

        if($save === false){
            DLOG("更新第三方分类信息失败 sql: " . $Dao->getLastSql(), "error", "channel_db");
            return false;
        }

        return $save;
    }

    /*
        通过第三方id获取分类信息
        params
            other_category_id 第三方分类id
    */
    public function getOtherInfoByOtherCategoryId($other_category_id = '')
    {
        $Dao = M("kfsy_channel_store.kf_channel_category");

        $where = sprintf("other_category_id = '%s'", $other_category_id);

        $categoryInfo = $Dao
            ->where($where)
            ->find();

        return $categoryInfo;
    }

    /*
        删除第三方分类信息
        params
            channel_id 第三方id
            store_id 店铺id
            other_category_id 第三方分类id
    */
    public function delCategoryInfo($channel_id = 0, $store_id = 0, $other_category_id = '')
    {
        $Dao = M("kfsy_channel_store.kf_channel_category");

        $where = array();

        $where[] = sprintf("fk_channel_id = %s", $channel_id);
        $where[] = sprintf("fk_store_id = %s", $store_id);
        $where[] = sprintf("other_category_id = %s", $other_category_id);

        $where = implode(" AND ", $where);

        $del = $Dao
            ->where($where)
            ->delete();

        if($del === false){
            DLOG("删除第三方分类失败 sql: ". $Dao->getLastSql(), "error", "channel_db");
            return false;
        }

        return $del;
    }

    /*
        批量替换第三方店铺分类
        params
            data 替换的数据对象数组
    */
    public function replaceChannelCategory($data = array())
    {
        $Dao = M("kfsy_channel_store.kf_channel_category");

        $add = $Dao->addAll($data, true);

        if($add === false){
            DLOG("替换第三方分类失败 sql: ". $Dao->getLastSql(), "error", "channel_db");
            return false;
        }

        return $add;
    }
}
?>